import 'package:flutter/cupertino.dart';

import '../container/radius_shadow_container.dart';

/// FileName nest_list_view
///
/// @Author lax
/// @Date 2023/6/28
///
/// @Description

class NestListView extends StatefulWidget {
  final int? sectionCount;

  final int Function(int section)? itemCount;

  final Widget Function(int section)? sectionHeaderBuilder;

  final Widget Function(int section, int index)? itemBuilder;

  final Widget Function(int section, int index)? separatedBuilder;

  final bool shrinkWrap;
  final ScrollPhysics? physics;

  final EdgeInsets? padding;

  final EdgeInsets? sectionPadding;

  final double sectionRadius;

  const NestListView({
    super.key,
    this.sectionCount,
    this.itemCount,
    this.sectionHeaderBuilder,
    this.itemBuilder,
    this.separatedBuilder,
    this.shrinkWrap = false,
    this.physics = const AlwaysScrollableScrollPhysics(),
    this.padding,
    this.sectionPadding,
    this.sectionRadius = 0,
  });

  @override
  State<StatefulWidget> createState() {
    return _NestListViewState();
  }
}

class _NestListViewState extends State<NestListView> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      shrinkWrap: widget.shrinkWrap,
      physics: widget.physics,
      padding: widget.padding ?? EdgeInsets.zero,
      itemCount: widget.sectionCount ?? 0,
      itemBuilder: (BuildContext context, int section) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            widget.sectionHeaderBuilder != null
                ? widget.sectionHeaderBuilder!(section)
                : const SizedBox(),
            RadiusShadowContainer(
              radius: widget.sectionRadius,
              padding: widget.sectionPadding ?? EdgeInsets.zero,
              child: sectionListView(section),
            ),
          ],
        );
      },
    );
  }

  Widget sectionListView(int section) {
    return ListView.separated(
      shrinkWrap: true,
      padding: EdgeInsets.zero,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: widget.itemCount != null ? widget.itemCount!(section) : 0,
      separatorBuilder: (_, index) {
        return widget.separatedBuilder != null
            ? widget.separatedBuilder!(section, index)
            : const SizedBox();
      },
      itemBuilder: (_, index) {
        return widget.itemBuilder != null
            ? widget.itemBuilder!(section, index)
            : const SizedBox();
      },
    );
  }
}
